package view.toolboxComponents;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JLabel;
import javax.swing.JTextField;
import controller.CustomEvent;
import controller.EventHandler;
import controller.ToolBoxEventHandler;
/** * A class to encapsulate the prescription height panel contents on the designer * @author Daniel Bond, Mohammed Abdullah, Gideon Pyzer * */
@SuppressWarnings("serial")
public class PrescriptionHeightPanel extends AbstractPanel{
	private JLabel lblPresHeight;
	private JTextField txtPresHeight;
	private EventHandler eventHandler;

	public PrescriptionHeightPanel(){		
		super();
		txtPresHeight = new JTextField();
		txtPresHeight.setMaximumSize(new Dimension(120, 20));
		lblPresHeight = new JLabel("Height");
		buildPanel();
		txtPresHeight.addKeyListener(new KeyListener(){
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ENTER){
					CustomEvent event = new CustomEvent("presHeightChange", PrescriptionHeightPanel.this,txtPresHeight.getText());
					PrescriptionHeightPanel.this.eventHandler = new ToolBoxEventHandler();
					eventHandler.createEvent(event);
				}
			}
			@Override
			public void keyReleased(KeyEvent arg0) {
			}
			@Override
			public void keyTyped(KeyEvent arg0) {
			}
		});
	}
	/**
	 * Set the text in the textfield to specified text
	 * @param text the text to set to the height 
	 */
	public void setText(String text){
		txtPresHeight.setText(text);
	}
	
	/**
	 * Build prescription name panel
	 */
	private void buildPanel() {		this.add(lblPresHeight);
		makeRigidArea();
		this.add(txtPresHeight);
	}
	
	public void resetComponent(){
		txtPresHeight.setText("");
	}
}